package czk.jvm.lecture.jvm;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/4 11:23
 */
public class Cmd {
    @Parameter(names = {"-?","-help"},description = "print help message" ,order = 3,help = true)
    boolean helpFlag=false;
    @Parameter(names = "-version",description = "print vision and exit",order = 2)
    boolean visionFlag=false;
    @Parameter(names = {"-cp", "-classpath"}, description = "classpath", order = 1)
    String classpath;
    @Parameter(description = "main class and args")
    List<String> mainClassAndArgs;
    boolean ok;
    String getMainClass(){
        return mainClassAndArgs!=null&&
                !mainClassAndArgs.isEmpty()?
                mainClassAndArgs.get(0):null;
    }
    List<String> getAppArgs(){
        return mainClassAndArgs!=null&&
                mainClassAndArgs.size()>0?
                mainClassAndArgs.subList(1,mainClassAndArgs.size()):null;
    }
    static Cmd parse(String[] argv){
        Cmd args=new Cmd();
        JCommander jCommander = JCommander.newBuilder().addObject(args).build();
        jCommander.parse(argv);
        args.ok=true;
        return args;
    }
}
